package com.yicong.ants.view.dialog;

import android.content.Context;

import androidx.recyclerview.widget.RecyclerView;

import com.cchao.simplelib.core.JsonHelper;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;
import com.yicong.ants.R;
import com.yicong.ants.bean.scenic.CityItemBean;

/**
 * 省份选择
 *
 * @author LinDingQiang
 * @description
 * @time 4/17/21 9:51 PM
 * @email dingqiang.l
 */
public class ProvinceDialog extends BaseNiceDialog {
    CallBack mListener;
    static Context mContext;
    String provinceJson = "[{\"city_id\":2,\"name\":\"北京\"},{\"city_id\":22,\"name\":\"安徽\"},{\"city_id\":149,\"name\":\"福建\"},{\"city_id\":245,\"name\":\"甘肃\"},{\"city_id\":347,\"name\":\"广东\"},{\"city_id\":530,\"name\":\"广西\"},{\"city_id\":654,\"name\":\"贵州\"},{\"city_id\":756,\"name\":\"海南\"},{\"city_id\":799,\"name\":\"河北\"},{\"city_id\":984,\"name\":\"河南\"},{\"city_id\":1167,\"name\":\"黑龙江\"},{\"city_id\":1310,\"name\":\"湖北\"},{\"city_id\":1431,\"name\":\"湖南\"},{\"city_id\":1569,\"name\":\"吉林\"},{\"city_id\":1643,\"name\":\"江苏\"},{\"city_id\":1781,\"name\":\"江西\"},{\"city_id\":1895,\"name\":\"辽宁\"},{\"city_id\":2012,\"name\":\"内蒙\"},{\"city_id\":2126,\"name\":\"宁夏\"},{\"city_id\":2158,\"name\":\"青海\"},{\"city_id\":2210,\"name\":\"山东\"},{\"city_id\":2369,\"name\":\"山西\"},{\"city_id\":2504,\"name\":\"陕西\"},{\"city_id\":2622,\"name\":\"上海\"},{\"city_id\":2643,\"name\":\"四川\"},{\"city_id\":2845,\"name\":\"天津\"},{\"city_id\":2866,\"name\":\"西藏\"},{\"city_id\":3060,\"name\":\"云南\"},{\"city_id\":3206,\"name\":\"浙江\"},{\"city_id\":3314,\"name\":\"重庆\"},{\"city_id\":2947,\"name\":\"新疆\"},{\"city_id\":3356,\"name\":\"香港\"},{\"city_id\":3376,\"name\":\"澳门\"}]";
    public static ProvinceDialog newInstance(Context context) {
        mContext = context;
        ProvinceDialog dialog = new ProvinceDialog();
        dialog.setOutCancel(true)
            .setShowBottom(true)
            .setDimAmount(0.3f);
        return dialog;
    }

    public ProvinceDialog setConvertListener(CallBack listener) {
        this.mListener = listener;
        return this;
    }

    @Override
    public int intLayoutId() {
        return R.layout.dialog_province;
    }

    @Override
    public void convertView(NiceViewHolder holder, BaseNiceDialog dialog) {
        RecyclerView recyclerView = holder.getView(R.id.rv_province);
        FlexboxLayoutManager flexboxLayoutManager = new FlexboxLayoutManager(mContext);
        flexboxLayoutManager.setFlexDirection(FlexDirection.ROW);//主轴为水平方向，起点在左端。
        flexboxLayoutManager.setFlexWrap(FlexWrap.WRAP);//按正常方向换行
        flexboxLayoutManager.setJustifyContent(JustifyContent.SPACE_BETWEEN);//交叉轴的起点对齐。
        recyclerView.setLayoutManager(flexboxLayoutManager);

        BaseQuickAdapter<CityItemBean, BaseViewHolder> mAdapter = new BaseQuickAdapter<CityItemBean, BaseViewHolder>(R.layout.dialog_province_item, JsonHelper.toList(provinceJson, CityItemBean.class)) {
            @Override
            protected void convert(BaseViewHolder helper, CityItemBean item) {
                helper.setText(R.id.tv_show_address, item.getName());
            }
        };
        recyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            CityItemBean itemBean = mAdapter.getData().get(position);
            mListener.onClick(itemBean);
            dismiss();
        });


    }

    public interface CallBack {
        void onClick(CityItemBean itemBean);
    }
}
